package algoritmoHiru;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Enumeration;

public class TaskB extends Thread{
	 public void run (){
		 try{
				byte [] buffer = new byte[0];
				DatagramSocket jasotzesocketa= new DatagramSocket(7777);
				DatagramPacket jasotzepaketea= new DatagramPacket(buffer,0);
				DatagramSocket bidaltzeSocketa=new DatagramSocket();
				//recovered arraya byte [] bihurtzeko
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
			    ObjectOutputStream oos = new ObjectOutputStream(bos);
			    oos.writeObject(Hasieraketa.recovered);
			    oos.writeObject(Hasieraketa.leader);
			    //**********************************************************
			    //KONTUZ BI objektu idazten dira eztakit funtzionatzen duen. Gero irakurtzeakuen 2 read eginda? lenengo arraye letzeko ta gero Stringe?
			    //**********************************************************
			    oos.flush();
			    oos.close();
			    bos.close();
			    byte [] mezua = bos.toByteArray();
			    /***************************************************/
			    System.out.println(mezua.length);
				DatagramPacket bidaltzepaketea=new DatagramPacket (mezua,mezua.length);
				bidaltzepaketea.setPort(7777);//PONG portua static bihurtu??????
			    
				MakinaBektorea makinak = MakinaBektorea.getInstance();
				String makinaIp;
				while(true){
					jasotzesocketa.receive(jasotzepaketea);
					bidaltzepaketea.setAddress(jasotzepaketea.getAddress());//byte array bezala pasa behar zaio ip-a
					bidaltzeSocketa.send(bidaltzepaketea);	
				}
				}catch(Exception e){
					e.printStackTrace();
				}
				
	 }
}
